package com.example.earthed.controller;

import com.example.earthed.model.Result;
import com.example.earthed.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping(path = "/app")
public class FileUploadController {
    private final MinioService minioService;

    @Autowired
    public FileUploadController(MinioService minioService) {
        this.minioService = minioService;
    }

    // 文件上传接口
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file,
                                     @RequestParam("bucketName") String bucketName,
                                     @RequestParam("objectName") String objectName) {
        try {
            minioService.uploadFile(file, bucketName, objectName);
            return Result.success("文件上传成功");
        } catch (Exception e) {
            return Result.error("文件上传时出现异常：" + e.getMessage());
        }
    }

    // 文件下载接口
    @GetMapping("/download/{bucketName}/{objectName}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable("bucketName") String bucketName,
                                               @PathVariable("objectName") String objectName) {
        try {
            InputStream inputStream = minioService.downloadFile(bucketName, objectName);
            if (inputStream == null) {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            byte[] bytes = inputStream.readAllBytes();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", objectName);
            return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 删除文件接口
    @DeleteMapping("/delete/{bucketName}/{objectName}")
    public Result<String> deleteFile(@PathVariable("bucketName") String bucketName,
                                     @PathVariable("objectName") String objectName) {
        try {
            minioService.deleteFile(bucketName, objectName);
            return Result.success("文件删除成功");
        } catch (Exception e) {
            return Result.error("文件删除时出现异常：" + e.getMessage());
        }
    }

    // 列出文件列表接口
    @GetMapping("/list/{bucketName}")
    public Result<List<String>> listFiles(@PathVariable("bucketName") String bucketName) {
        try {
            List<String> fileList = minioService.listFiles(bucketName);
            return Result.success(fileList);
        } catch (Exception e) {
            return Result.error("获取文件列表时出现异常：" + e.getMessage());
        }
    }
}